﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web.UI;

/// <summary>
///CommonUse 的摘要说明
/// </summary>
public class CommonUse
{
  public CommonUse()
  {
    //
    //TODO: 在此处添加构造函数逻辑
    //
  }

  /// <summary>
  /// 检查数字
  /// </summary>
  /// <param name="txt"></param>
  /// <returns></returns>
  public static bool IsInt(TextBox txt)
  {
    bool flag = false;
    //if (string.IsNullOrEmpty(txt.Text))
    //{
    //  flag = true;
    //  return flag;
    //}
    try
    {
      Int32.Parse(txt.Text);
      flag = true;
    }
    catch
    {
      txt.Focus();
      //txt.Text = "请输入有效数字";
      flag = false;
    }
    return flag;
  }
  public static bool IsInt(string txt)
  {
    bool flag = false;
    //if (string.IsNullOrEmpty(txt.Text))
    //{
    //  flag = true;
    //  return flag;
    //}
    try
    {
      Int32.Parse(txt);
      flag = true;
    }
    catch
    {
      //txt.Text = "请输入有效数字";
      flag = false;
    }
    return flag;
  }

  public static bool IsDouble(TextBox txt)
  {
    bool flag = false;
    //if (string.IsNullOrEmpty(txt.Text))
    //{
    //  flag = true;
    //  return flag;
    //}
    try
    {
      double.Parse(txt.Text);
      flag = true;
    }
    catch
    {
      txt.Focus();
      //txt.Text = "请输入有效数字";
      flag = false;
    }
    return flag;
  }

  public static string GetID()
  {
    string sID = "";
    if (HttpContext.Current.Request.QueryString["id"] != null)
    {
      sID = HttpContext.Current.Request.QueryString["id"].ToString();
    }
    return sID;
  }

  public static string GetID(string sRID)
  {
    string sID = "";
    if (HttpContext.Current.Request.QueryString[sRID] != null)
    {
      sID = HttpContext.Current.Request.QueryString[sRID].ToString();
    }
    return sID;
  }

  public static string GetID(string sTable, string sColumn)
  {
    string sID = "1";
    string sSql = "select isnull(max(" + sColumn + ")+1,'1') id from " + sTable;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql);
    if (dr.HasRows)
    {
      dr.Read();
      sID = dr["id"].ToString();
    }
    dr.Close();
    return sID;
  }

  public static bool ExistID(string sTable, string sColumn, string sText)
  {
    bool flag = false;
    string sSql = "select " + sColumn + " from " + sTable + " where " + sColumn + "=@0";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = sText;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    if (dr.HasRows)
    {
      flag = true;
    }
    dr.Close();
    return flag;
  }

  public static CommandInfo DeleteCI(string sTable, string sColumn, string sID)
  {
    string sSql = "delete from " + sTable + " where " + sColumn + "=@0";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = sID;
    CommandInfo ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    return ci;
  }

  public static CommandInfo InsertCI(string sTable, ArrayList arrValue, ArrayList arrLengh)
  {
    string sSql = "insert into  " + sTable + " values(";
    SqlParameter[] cmdParams = new SqlParameter[arrValue.Count];

    for (int i = 0; i < arrValue.Count; i++)
    {
      sSql = sSql + "@" + i.ToString() + ",";
      SqlParameter cmdParam = new SqlParameter("@" + i.ToString(), SqlDbType.VarChar, Convert.ToInt32(arrLengh[i].ToString()));
      cmdParam.Value = arrValue[i];
      cmdParams[i] = cmdParam;
    }
    sSql = sSql.Trim(new char[] { ',' });
    sSql = sSql + ")";

    CommandInfo ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    return ci;
  }

  public static void Alert(string s)
  {
    System.Web.UI.Page page = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
    ClientScriptManager cs = page.ClientScript;
    cs.RegisterStartupScript(typeof(string), "TabJs", "<script language='javascript'>alert( '" + s + "'); </script>");
  }

  //月初时间
  //public static string GetYC()
  //{
  //  string s = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-01";
  //  return s;
  //}

  ////月末时间
  //public static string GetYM()
  //{
  //  string s = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString()
  //      + "-" + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month).ToString();
  //  return s;
  //}

  //月初时间
  public static DateTime GetYC()
  {
    string s = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-01";
    return Convert.ToDateTime(s);
  }

  //月末时间
  public static DateTime GetYM()
  {
    string s = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString()
        + "-" + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month).ToString();
    return Convert.ToDateTime(s);
  }

  //绑定dll
  public static void BindDDL(DataSet ds, string value, string text, string select, DropDownList ddl)
  {
    ddl.DataTextField = text;
    ddl.DataValueField = value;
    ddl.DataSource = ds;
    ddl.DataBind();
    ddl.SelectedValue = select;
  }

}
